// Custom sorting plugin
(function($) {
  $.fn.sorted = function(customOptions) {
    var options = {
      reversed: false,
      by: function(a) { return a.text(); }
    };
    $.extend(options, customOptions);
    $data = $(this);
    arr = $data.get();
    arr.sort(function(a, b) {
      var valA = options.by($(a));
      var valB = options.by($(b));
      if (options.reversed) {
        return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;				
      } else {		
        return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;	
      }
    });
    return $(arr);
  };
})(jQuery);

// Main Menu
function ypcore_mainmenu(){
$("#nav-menu ul").css({display: "none"}); // Opera Fix
$("#nav-menu>li>a").append('<span class="dot nav"></span>');
$("#nav-menu ul").find('a').append('<span class="dot subnav"></span>');
$("#nav-menu>li>ul").find('ul').prev().append('<span class="more-items"><i class="icon16 icon-arrow-down"></i></span>');
$("#nav-menu li").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(0);
		},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
		});
}

$.preloadImages = function()
{
   for(var i = 0; i<arguments.length; i++)
   {
        $("<img />").attr("src", arguments[i]);
   }
}

// To top
function ypCore_gotop() {
	$('body,html').animate({
		scrollTop: 0
	}, 800);
	return false;
}


// document ready function
$(document).ready(function() {
	//******************** Tools tip ******************//
	$('#cat-box-view, #cat-line-view, .icon-bullhorn, .box-cat .head h4 a, .line-cat .head h4 a').tooltip();

	
	//******************** Initialize main menu ******************//
	ypcore_mainmenu();

	//******************** Back to top ******************//
	// hide #back-top
	$("#back-top").hide();
	
	$(function () {
		$(window).scroll(function () {
			if ($(this).scrollTop() > 100) {
				$('#back-top').fadeIn();
			} else {
				$('#back-top').fadeOut();
			}
		});

		// scroll body to 0px on click
		$('#back-top a').click(function () {
			ypCore_gotop();
		});
		
	});
		// scroll to top by press 't'
		document.addEventListener("keydown", function(e){
			if (e.keyCode == 84) { // 't' --- Go to top
				ypCore_gotop();
			} else if (e.keyCode == 66) { // 'b'
				//ypCore_gobottom();
				alert('Go to bottom site!');
			} else if (e.Keycode == 72) { // 'h' --- Help
				alert(ypCore_jsphrase_help_shortcuts);
			}
		}, false);

	//******************** Media player ******************//
    $('audio,video').mediaelementplayer();

	//******************** Form validation plugin ******************//
	// validate the comment form when it is submitted
	$(".form-comment").validate();
	$(".form-contact").validate();

	//******************** Misc ******************//

	// Placeholder 
	$('input , textarea').watermark();
	
	//css animation for links
	$('.links').addClass('animated fadeInDown');

	$('.social-links').addClass('animated fadeInLeft');

	//css animation for headlines
	$('.view-all').addClass('animated fadeInLeftBig');

	//css animation for box icons
	$('.box').hover(function(){
		$(this).find('.center.i').addClass('animated swing');
	},function(){
		$(this).find('.center.i').removeClass('animated swing');
	});

	//css animation for service icons
	$('#services .span3').hover(function(){
		$(this).find('i').addClass('animated swing');
	},function(){
		$(this).find('i').removeClass('animated swing');
	});

	//error page animation
	/*$('#errorPage .center i').addClass('animated pulsar');*/

	// make code pretty
    window.prettyPrint && prettyPrint()

    // add tipsies to grid for scaffolding
    if ($('#grid-system').length) {
      $('#grid-system').tooltip({
          selector: '.show-grid > div'
        , title: function () { return $(this).width() + 'px' }
      })
    }
});